package com.example.bookkeeping1220;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.example.bookkeeping1220.R;
import com.example.bookkeeping1220.db.AccountBean;
import com.example.bookkeeping1220.db.AchievementBean;
import com.example.bookkeeping1220.db.DBManager;
import com.example.bookkeeping1220.utils.BudgetDialog;
import com.example.bookkeeping1220.HeadportraitActivity;
import com.example.bookkeeping1220.utils.NameDialog;

public class UserActivity extends AppCompatActivity {
    TextView nameTv;
    TextView signTv;
    TextView achievementTv;
    ImageView headIv;
    Button achievementBtn;
    Button scoreBtn;
    SharedPreferences preferences;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_individual);
        nameTv=findViewById(R.id.username_user);
        signTv=findViewById(R.id.sign_user);
        headIv=findViewById(R.id.head_portrait);
        achievementBtn=findViewById(R.id.user_achievement);
        achievementTv=findViewById(R.id.user_simple_achievement);
        scoreBtn=findViewById(R.id.user_score);
        preferences = getSharedPreferences("修改信息", Context.MODE_PRIVATE);
        if(DBManager.getUserName() != null){
            nameTv.setText(DBManager.getUserName());
        }
        if(DBManager.getUserSign() != null){
            signTv.setText(DBManager.getUserSign());
        }
        if(DBManager.getUserHead() != -1){
            headIv.setImageResource(DBManager.getUserHead());
        }
        int achieveNum = AchievementActivity.getAchievementNum();
        achievementTv.setText("成就达成："+achieveNum);
        scoreBtn.setText("当前积分为："+DBManager.getUserScore());
        setNameTvLongClickListener();
        setSignTvLongClickListener();
        setHeadIvLongClickListener();

        // ZWX
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);

        bottomNavigationView.setSelectedItemId(R.id.person);

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuitem) {
                switch (menuitem.getItemId()){
                    case R.id.bill:
                        startActivity(new Intent(getApplicationContext(),MainActivity.class));
                        overridePendingTransition(0,0);
                        return true;

                    case R.id.chat:
                        startActivity(new Intent(getApplicationContext(),CharactersActivity.class));
                        overridePendingTransition(0,0);
                        return true;

                    case R.id.person:
                        return true;
                }
                return false;
            }
        });
    }

    public void onClick(View view) {
        Intent it = new Intent();
        switch (view.getId()){
            case R.id.user_achievement:
                it = new Intent(this, AchievementActivity.class);  //跳转界面
                startActivity(it);
                break;
            case R.id.user_score:
                int theScore = DBManager.getUserScore();
                Toast toastCenter = Toast.makeText(UserActivity.this,"当前积分为："+DBManager.getUserScore(),Toast.LENGTH_LONG);
                toastCenter.setGravity(Gravity.CENTER,0,0);
                toastCenter.show();
                break;
            case R.id.user_billrecord:
                it = new Intent(this, HistoryActivity.class);  //跳转界面
                startActivity(it);
                break;
            case R.id.user_setting:
                it = new Intent(this,SettingActivity.class);  //跳转界面
                startActivity(it);
                break;
            case R.id.user_about:
                it = new Intent(this, AboutActivity.class);  //跳转界面
                startActivity(it);
                break;
            case R.id.dialog_more_iv:
                break;


        }

    }

    private void setNameTvLongClickListener() {
        nameTv.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                longClickToChangeTheText(0);
                return true;
            }
        });
    }

    private void setSignTvLongClickListener() {
        signTv.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                longClickToChangeTheText(1);
                return true;
            }
        });
    }

    private void setHeadIvLongClickListener() {
        headIv.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Intent it = new Intent(UserActivity.this, HeadportraitActivity.class);  //跳转界面
                startActivity(it);
                return true;
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        if(DBManager.getUserName() != null){
            nameTv.setText(DBManager.getUserName());
        }
        if(DBManager.getUserSign() != null){
            signTv.setText(DBManager.getUserSign());
        }
        if(DBManager.getUserHead() != -1){
            headIv.setImageResource(DBManager.getUserHead());
        }
    }

    private void longClickToChangeTheText(int flag) {
        if(flag == 0){
            NameDialog dialog = new NameDialog(this);
            dialog.show();
            dialog.setDialogSize();
            dialog.setOnEnsureListener(new NameDialog.OnEnsureListener() {
                @Override
                public void onEnsure(String name) {
                    nameTv.setText(name);
                    DBManager.setName(name);
                }
            });
        }else if(flag == 1){
            NameDialog dialog = new NameDialog(this);
            dialog.show();
            dialog.setDialogSize();
            dialog.setOnEnsureListener(new NameDialog.OnEnsureListener() {
                @Override
                public void onEnsure(String sign) {
                    signTv.setText(sign);
                    DBManager.setSign(sign);
                }
            });
        }
    }
}
